package logobj.ftpUtil;

import logobj.config.ConfProperties;
import org.apache.commons.net.ftp.FTPClient;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class FtpDownload {
    private static FTPClient ftpClient = null;
    private static Map<String , ArrayList> ftpfilemap =null;
    private static ArrayList namelist = new ArrayList();
    private static ArrayList sizelist = new ArrayList();
    //初始化
    public static void init(){
        //连接配置
        FtpUtil.FtpConfig ftpConfig = new FtpUtil.FtpConfig().setUserName(ConfProperties.ftp_username)
                .setPassword(ConfProperties.ftp_pwd).setIp(ConfProperties.ftp_ip).setPort(ConfProperties.ftp_port);
        //下载文件
        try {
             ftpClient = FtpUtil.connectClient(ftpConfig);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //下载文件
    public static Map<String,ArrayList> loadfile(){
         ftpfilemap = FtpUtil.getFtpFileNameSize(ftpClient);
        ArrayList namelist1 = ftpfilemap.get("namelist");
        ArrayList sizelist1 = ftpfilemap.get("sizelist");
         String filename ="";
        for(int i = 0;i<namelist1.size();i++){
            filename = String.valueOf(namelist1.get(i));
            if(filename.indexOf(".log") != -1){
                FtpUtil.download(ftpClient, ConfProperties.ftp_path, filename , ConfProperties.ftp_localpath, filename );
                namelist.add(namelist1.get(i));
                sizelist.add(sizelist1.get(i));
            }
        }
        Map<String,ArrayList> fineinfomap = new HashMap<>();

        fineinfomap.put("namelist",namelist);
        fineinfomap.put("sizelist",sizelist);
        return fineinfomap;
    }
    //清空ftp文件
    public static void clearfile(){
        FtpUtil.deleteFile(ftpClient,ConfProperties.ftp_path,namelist);
    }
    //关闭ftp
    public static void closeftp(){
        FtpUtil.LoutOffer(ftpClient);
    }

}
